package com.taotao.portal.controller;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.portal.pojo.SearchResult;
import com.taotao.portal.service.SearchServer;

@Controller
public class SearchController {
	@Autowired
	private SearchServer searchServer;
	@RequestMapping("/search")
	public String querySearch(@RequestParam("q") String query,
			@RequestParam(value="page",defaultValue="1") Integer page,Map<String,Object> map){
		if(query!=null){
			try {
				query=new String(query.getBytes("iso8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		SearchResult searchResult = searchServer.searchItem(query, page);
		map.put("query", query);
		map.put("totalPages", searchResult.getCountPage());
		map.put("itemList", searchResult.getItems());
		map.put("page",page);
		return "search";
	}
}
